//
//  DestinationDetailViewController.m
//  LT
//
//  Created by dllo on 16/5/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DestinationDetailViewController.h"
#import "ZHNetworking.h"
#import "ZHScrollView.h"
#import "DestinationDetailModel.h"
#import "detailsModel.h"
#import "discountModel.h"
#import "DestinationDetailTableViewCell.h"
#import "discountTableViewCell.h"


static NSString *const detailsCell = @"DetailsCELL";
static NSString *const discountCell = @"DiscountCELL";



@interface DestinationDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *detailsTableView;
@property(nonatomic,strong)UIView *HeaderView;
@property(nonatomic,strong)NSMutableArray *photoArray;
@property(nonatomic,strong)NSMutableArray *AllArray;
@property(nonatomic,strong)NSMutableArray *titleArray;

@property(nonatomic,strong)ZHScrollView *zhSV;


@end

@implementation DestinationDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //初始化数组(不初始化后面会崩溃)
    self.photoArray = [NSMutableArray array];
    self.AllArray = [NSMutableArray array];
    self.titleArray = [NSMutableArray array];
    
    self.detailsTableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    [self.view addSubview:self.detailsTableView];
    self.detailsTableView.delegate = self;
    self.detailsTableView.dataSource = self;
    
    //注册
    [self.detailsTableView registerClass:[DestinationDetailTableViewCell class] forCellReuseIdentifier:detailsCell];
    [self.detailsTableView registerClass:[discountTableViewCell class] forCellReuseIdentifier:discountCell];
    
    
}

-(void)setHotCount:(NSMutableArray *)hotCount{
    _hotCount = hotCount;

}

//取数据
-(void)getData{
    NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/country_detail?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=867323023990045&track_app_version=6.7.1&track_app_channel=xiaomi&track_device_info=cancro&track_os=Android4.4.4&app_installtime=1443411950696&lat=38.883818&lon=121.544801&country_id=%@",self.string];
    [ZHNetworking zhGETWithURL:url parameter:nil succes:^(id result) {
        
        NSDictionary *dic = result;
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        DestinationDetailModel *destinstion = [[DestinationDetailModel alloc]init];
        [destinstion setValuesForKeysWithDictionary:dataDic];
        [self.titleArray addObject:destinstion];
        [self.AllArray addObject:destinstion.hot_city];
        [self.AllArray addObject:destinstion.discount];
        
        self.photoArray = destinstion.photos;
        
        //添加头视图
        self.HeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300* WIDTHCELL)];
        //设置成头视图
        self.detailsTableView.tableHeaderView = self;
        
      //此处调用轮播图方法
        
        
        //刷新tableView
        [self.detailsTableView reloadData];
        
    } failure:^(NSError *error) {
        
        
        
    }];



}


//轮播图方法(需要加轮播图文字)
-(void)getScrollViewDataWith:(NSString *)title{
    ZHScrollView *destinstionSV = [[ZHScrollView alloc]initWithFrame:self.HeaderView.frame];
    destinstionSV = destinstionSV;
    
    //调用头视图按钮方法
    [self getButtonWith:title];
    [self.HeaderView addSubview:destinstionSV];


}

//添加头视图按钮
-(void)getButtonWith:(NSString *)title{
    UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [returnButton setImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    returnButton.frame = CGRectMake(10* WIDTHCELL, 10* WIDTHCELL, 50* WIDTHCELL, 50* WIDTHCELL);
    [self.zhSV addSubview:returnButton];
    [returnButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    titleButton.backgroundColor = [UIColor blackColor];
    titleButton.alpha = 0.3;
    [titleButton setTitle:title forState:UIControlStateNormal];
    titleButton.titleLabel.numberOfLines = 3;
    titleButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    titleButton.frame = CGRectMake(0, 200* WIDTHCELL, self.zhSV.frame.size.width, 100* WIDTHCELL);
    [titleButton addTarget:self action:@selector(titleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.zhSV addSubview:titleButton];



}

-(void)titleButtonAction:(UIButton *)button{



}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"城市";
    }
return @"超值自由行";

}


//section数量就是allArray的数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return self.AllArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 250* WIDTHCELL;
    }
    return 100* WIDTHCELL;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    NSArray *tempArray = [self.AllArray objectAtIndex:section];
    return tempArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        discountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:discountCell];
        cell.tempArray = [self.AllArray objectAtIndex:indexPath.section];
        return cell;
        
    }
    DestinationDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:detailsCell];
    NSArray *tempArray = [self.AllArray objectAtIndex:indexPath.section];
    discountModel *discount = [tempArray objectAtIndex:indexPath.row];
    cell.dismodel = discount;
    return cell;


}


//界面跳回
-(void)leftButtonAction:(UIButton *)item{

[self dismissViewControllerAnimated:YES completion:^{
    
    
}];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
